
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)
import {getUserInfo,collection} from "../api/user-api"
import {getSearch} from "../api/GoodNews";

export default new Vuex.Store({
  state: {
    islogin: false,
    loginName: "",
    userName:"",
    phonenumber:"",
    avatar:"",
    //新闻列表
    NewsList:[],
    //新闻上传时间
    SendTime:'',
    //收藏
    favorite:"",
    //赞的数量
    zanNumber:'',
    //搜索获得到的内容
    SearchContent:[],
    //搜索的标题
    SearchTitle:'',
  //  订阅列表
    SubList:[],
    //已订阅列表
    SubListRight:[],
    //  存储的页面切换点击状态
    bangFlag : false,
    musicFlag : false,
    myFlag : false,
  //  收藏列表
    collectionList:[]
  },

  mutations: {

    changelogin(state, payload) {
      state.islogin = payload.islogin
      state.loginName = payload.loginName || "";
      state.userName = payload.userName || "";
      state.phonenumber = payload.phonenumber || "";
      state.avatar = payload.avatar || "";
    },
    //储存新闻列表
    changeNewsList(state,payload){
      // state.isLogin = payload.isLogin
      state.NewsList = payload.NewsList || [];
    },
    //储存新闻发布时间
    ReSendTime(state,payload){
      state.SendTime = payload.SendTime;
    },

    favorite(state,payload){
      state.favorite = payload.favorite;
    },

    //储存赞的数量
    ReZanNumber(state,payload){
      state.zanNumber = payload.zanNumber || "0";
    },
//储存搜索到的内容
    ReSearchContent(state,payload){
      state.SearchTitle = payload.SearchTitle || ''
      state.SearchContent = payload.SearchContent || [];
    },
    // //储存搜索到的标题
    // ReSearchTitle(state,payload){
    //   state.SearchTitle = payload.SearchTitle || ''
    //   state.SearchContent = payload.SearchContent || [];
    // },

    //储存未订阅列表
    ReSubList(state,payload){
      state.SubList = payload.SubList || [];
    },
    //储存已订阅列表
    ReSubListRight(state,payload){
      state.SubListRight = payload.SubListRight || [];
    },

    //收藏列表
    ReCollection(state,payload){
      state.collectionList = payload.collectionList || []
    },
  },
  actions: {
    //获取用户信息，判断是否登录
    getUserInfo1({commit}){
      return getUserInfo().then(res => {
        // console.log(res)

        const payload = {
          islogin: false,
          loginName: "",
          userName:"",
          phonenumber:"",
          avatar:""
        };
        if (res.code === 0) {
          payload.islogin = true;
          payload.loginName = res.data.loginName;
          payload.userName = res.data.userName;
          payload.phonenumber = res.data.phonenumber;
          payload.avatar = res.data.avatar;
        }
        commit("changelogin", payload);
        // return res;
      })
    },
    //获取搜索内容，刷新页面也存在
    getSearchListaTitle({commit},payload){
      return getSearch(1,10,payload.SearchTitle).then(res => {

        payload.SearchContent = res.rows;
        payload.SearchTitle = payload.SearchTitle
        // console.log(payload.SearchContent)
        // console.log(payload.SearchTitle)
        commit("ReSearchContent", payload);
        // return res;
      })
    },

    //获取搜索内容，刷新页面也存在
    getCollectionList({commit}){
      return collection().then(res => {
        // console.log(this.SearchTitle)
        const payload = {
          //搜索获得到的内容
          collectionList:[],
        };
        payload.collectionList = res.rows;
        commit("ReCollection", payload);
      })
    },

  },})

